var editId
// 上传图片函数 监听input
$('#select-images').change(function() {
    var files = this.files;
    // 获取目前上传的文件，防止为空
    if(files && files.length>0) {
        fil = files[0];
        var URL = window.URL || window.webkitURL;
        var imageUrl = URL.createObjectURL(fil);
        $('.xl-images').attr('src',imageUrl)
    }
})


// 选择老师下拉框设置
$('.xl-select-teacher').on('click',function() {
    $('.glyphicon-chevron-down').toggle();
    $('.glyphicon-chevron-up').toggle();
    $('.xl-teacher').slideToggle()
})
// 老师选择成功赋予给value
$('.xl-teacher').on('click','.xl-pro',function(e) {
    $('.xl-select-teacher').val($(this).html())
    $('.xl-teacher').hide()
    $('.glyphicon-chevron-down').show();
    $('.glyphicon-chevron-up').hide();
})

// 选择班级下拉框设置
$('.xl-select-class').on('click',function() {
    $('.glyphicon-chevron-down').toggle();
    $('.glyphicon-chevron-up').toggle();
    $('.xl-class').slideToggle()
})
// 班级选择成功赋予给value
$('.xl-class').on('click','.xl-pro',function(e) {
    $('.xl-select-class').val($(this).html())
    $('.xl-class').hide()
    $('.glyphicon-chevron-down').show();
    $('.glyphicon-chevron-up').hide();
})

// 初始化页面

function init() {
    editId = location.search? location.search.substr(1) : ''
    editId = Number(editId)
    console.log('editId', editId)
    if(editId) {
       $('#courseId').prop('disabled', true);
       return getCourseInfo()
    }
}

init()


// 提交数据
$('.xl-save').on('click',function() {
    console.log('保存成功')
    if(editId) {
        editSubmit()
    } else {
        addSubmit()
    }
    var allInputs = document.getElementsByClassName('form-control')
    // 所有value 清零
    for(var i=0;i<allInputs.length;i++) {
        allInputs[i].value = '';
        $(allInputs[4]).next().children('.xl-images').attr('src','');

    }
})

// 获得学生信息
function getCourseInfo() {
    $.ajax({
        url:'/courses/selectid',
        method:'GET',
        data:{
            editId
        }
    })
     .done(function(res) {
        console.log('传过来的数据',res.data.courseCreateTime)
        let courseName = $('#courseName').val(res.data.courseName);
        let courseIntroduction = $('#courseIntro').val(res.data.courseIntroduction);
        let courseContent = $('#courseCon').val(res.data.courseContent)
        let courseTeacher = $('#courseTeach').val(res.data.courseProfessor)
        let courseImage = $('.xl-images').prop('src','../../images/lxm_images/ca120.png')
        let courseNumber = $('#courseId').val(res.data.courseNumber);
        let courseCreateTime = $('#courseCretime').val(res.data.courseCreateTime);
        let courseStartTime = $('#courseStarsTime').val(res.data.courseStartTime);
        let courseDuration = $('#courseDuration').val(res.data.courseDuration);
        let courseClass = $('#courseClass').val(res.data.courseClass);
        let courseAddress = $('#courseAddress').val(res.data.courseAddress);
     })
     .fail(function(err) {
        console.log('请求课程详细信息失败：', err);
     })
}
// 添加
function addSubmit() {
    console.log($('#courseName').val())
    let courseName = $('#courseName').val();
    let courseIntroduction = $('#courseIntro').val();
    let courseContent = $('#courseCon').val()
    let courseTeacher = $('#courseTeach').val()
    let courseImage = '../../images/lxm_images/ca120.png'
    let courseNumber = $('#courseId').val();
    let courseCreateTime = $('#courseCretime').val();
    let courseStartTime = $('#courseStarsTime').val();
    let courseDuration = $('#courseDuration').val();
    let courseClass = $('#courseClass').val();
    let courseAddress = $('#courseAddress').val();

    let obj = {
        courseName,
        courseIntroduction,
        courseContent,
        courseTeacher,
        courseImage,
        courseNumber,
        courseCreateTime,
        courseStartTime,
        courseDuration,
        courseClass,
        courseAddress
    }
    $.ajax({
        url:'/courses/add',
        method:'POST',
        data:obj
    })
     .done(function(res) {
         console.log(res.msg,'kkk',res.err)
     })
     .fail(function(err) {
         console.log(err.msg,'hhh',err.err)
     })
}

// 编辑
function editSubmit() {
    let courseName = $('#courseName').val();
    let courseIntroduction = $('#courseIntro').val();
    let courseContent = $('#courseCon').val();
    let courseTeacher = $('#courseTeach').val();
    let courseImage =  '../../images/lxm_images/ca120.png'
    let courseNumber = $('#courseId').val();
    let courseCreateTime = $('#courseCretime').val();
    let courseStartTime = $('#courseStarsTime').val();
    let courseDuration = $('#courseDuration').val();
    let courseClass = $('#courseClass').val();
    let courseAddress = $('#courseAddress').val();

    let obj = {
        courseName,
        courseIntroduction,
        courseContent,
        courseTeacher,
        courseImage,
        courseNumber,
        courseCreateTime,
        courseStartTime,
        courseDuration,
        courseClass,
        courseAddress
    }
    $.ajax({
        url:'/courses/edit',
        method:'POST',
        data:obj
    })
     .done(function(res) {
         console.log(res.msg,'bbb',res.err)
     })
     .fail(function(err) {
         console.log(err.msg,err.err)
     })
}

$('.xl-cancel').on('click',function() {
    var allInputs = document.getElementsByClassName('form-control')
    // 所有value 清零
    for(var i=0;i<allInputs.length;i++) {
        allInputs[i].value = '';
        $(allInputs[4]).next().children('.xl-images').attr('src','');

    }
})
// 返回
$('.xl-add-return').on('click',function() {
    parent.$('.myiframe').prop('src','lxm_pages/all_course.html')
})

// 得到本地teacher数据
// var prof = loaclGet('teachers')
// renderTeacher(prof)
// // 动态创建老师
// function renderTeacher(data) {
//     $('.xl-teacher').html('')

//     for (var i = 0; i < data.length; i++) {
//         var li = `
//         <li class="xl-pro">${data[i].name}</li>
//         `;
//         $('.xl-teacher').append($(li));
//       }
// }
